package com.cmol.peopledetectapp.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cmol.data.SettingData;
import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;
import com.cmol.peopledetectapp.json.Scene;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 场景列表
 *
 * @author 邢文成 王国良
 */
public class ListLocationActivity extends AppCompatActivity {

    /**
     * 通信模块
     */
    private Network network;
    private long exitTime;
    /**
     * 列表子项
     */
    private List<BeanLocation> data = new ArrayList<BeanLocation>();
    /**
     * 传输参数
     */
    private Intent intent;
    /**
     * 界面上的选择按钮
     */
    private Button personButton, listButton, mapButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_location);

        personButton = findViewById(R.id.PersonButton1);
        listButton = findViewById(R.id.button5);
        mapButton = findViewById(R.id.MapButton1);
        /**
         * 设置系统主题
         */
        systemSetting();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        super.onStart();
        ListLocationActivity tmp = this;
        /**
         * 网络连接
         */
        JSONObject jsonObject = new JSONObject();
        data = new ArrayList<BeanLocation>();
        network = new Network(jsonObject, "/user/sceneList") {
            /**
             * 200OK
             *
             * @param call
             * @param response 响应报文
             */
            @Override
            protected void process(Call call, Response response) {
                try {
                    final String body = response.body().string();
                    JSONObject res = new JSONObject(body);
                    String array = res.getString("sceneList");
                    Gson gson = new Gson();
                    Type sceneListType = new TypeToken<ArrayList<Scene>>() {
                    }.getType();
                    List<Scene> sceneList = gson.fromJson(array, sceneListType);
                    if (sceneList.size() != 0) {
                        for (Scene scene : sceneList) {
                            BeanLocation beanLocation = new BeanLocation();
                            beanLocation.setName(scene.getName());
                            data.add(beanLocation);
                        }
                    }
                } catch (IOException | JSONException e) {
//                    e.printStackTrace();
                }
                intent = new Intent(tmp, DetailActivity.class);
//                setView();
            }

            /**
             * 链接终端或失败
             * 显示网络异常提示
             */
            @Override
            protected void failureWarn() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListLocationActivity.this, "请求场景列表错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
//            e.printStackTrace();
        }
        ListView listView = findViewById(R.id.LocationList);
        listView.setAdapter(new MyAdapterLocation(data, tmp));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // position 为被点击的表项序号
                startActivity(intent);

            }
        });
    }

    private void setView() {
    }

    //按两次返回才退出
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 功能选择
     * 地图
     *
     * @param view
     */
    public void MapClick(View view) {
        startActivity(new Intent(this, MapActivity.class));
        finish();
    }

    /**
     * 功能选择
     * 个人信息
     *
     * @param view
     */
    public void PersonClick(View view) {
        startActivity(new Intent(this, PersonalActivity.class));
        finish();
    }

    /**
     * 主题设置
     */
    private void systemSetting() {
        if (SettingData.getCOLOR() == false) {
            personButton.setBackgroundResource(R.drawable.ic_baseline_redengineering_24);
            listButton.setBackgroundResource(R.drawable.ic_baseline_redclear_all_24);
            mapButton.setBackgroundResource(R.drawable.ic_baseline_redlocation_on_24);
        } else {
            personButton.setBackgroundResource(R.drawable.ic_baseline_engineering_24);
            listButton.setBackgroundResource(R.drawable.ic_clear_all_black_24dp);
            mapButton.setBackgroundResource(R.drawable.ic_place_black_24dp);
        }
    }
}